var aMailServices = angular.module('picture', ['ngRoute','ngAnimate']);
var baseUrl ="http://bgy2.lly360.com/ws";
var autoJump ;
//配置路由
function emailRouteConfig($routeProvider) {
	//首页
	
	$routeProvider.when('/', {controller: StoryController,templateUrl:'story.html'})
	
	.when('/main', {controller: MainController,templateUrl:'main.html'})
	
	.when('/upload', {controller: UploadController,templateUrl:'upload.html'})
	
	.when('/match', {controller: MatchController,templateUrl:'match.html'})
	
	.when('/share/:shareId', {controller: ShareController,templateUrl:'share.html'})
	
	.otherwise({redirectTo: '/'});
}


aMailServices.config(emailRouteConfig);


//根controller
function IndexController($scope,$rootScope,$location,$window,$http) {
		//页面切换
		
	if($location.href=='undefined'||$location.href==undefined||$location.href==""||$location.href==null)
	{
		
		hideSpin();
	}
	else
	{
		
		showSpin();
	}
	
		$rootScope.go = function (path, pageAnimationClass) {
		//页面成功切换回调函数   main.js
			
		
				
			
		//页面切换效果
	    if (typeof(pageAnimationClass) === 'undefined') { // Use a default, your choice
	        $rootScope.pageAnimationClass = 'fade';
	        
	    }
	         
	    else { // Use the specified animation
	        $rootScope.pageAnimationClass = pageAnimationClass;
	        
	    }
	 
	    if (path === 'back') { // Allow a 'back' keyword to go to previous page
	        $window.history.back();
	    }
	         
	    else { // Go to the specified path
	        $location.path(path);
	    }
	};
	//加上一个参数的方法 --页面切换
	$rootScope.goParameter = function (path, pageAnimationClass,parameter) {
 	
		//页面成功切换回调函数   main.js
		//页面切换效果
	    if (typeof(pageAnimationClass) === 'undefined') { // Use a default, your choice
	        $rootScope.pageAnimationClass = 'slide';
	    }else { // Use the specified animation
	        $rootScope.pageAnimationClass = pageAnimationClass;
	    }
	 
	    if (path === 'back') { // Allow a 'back' keyword to go to previous page
	        $window.history.back();
	    }else { // Go to the specified path
	        $location.path(path+"/"+parameter);
	    }
	};
	
	
    playbksound();
}


//首页controller
function StoryController($scope,$rootScope,$location,$http,$timeout) {
//	var local = "%2Fhtml%2Fpicture%2Findex.html%23%2F";from=singlemessage&isappinstalled=0#/?a=1

	autoJump = $timeout(function(){
		//$scope.go('/main');
		$location.url('/main');
	},12000);

	hideSpin();
	var local = location.href;
	local = encodeURIComponent(local);
	var url = baseUrl+"/index.php?g=mobile&m=picture&a=get_wx_info&url="+local;
    $http.get(url).success(function (res) {
    	wxConfig(res);
    	hideSpin();
    	
    });
}


//首页controller
function MainController($scope,$rootScope,$location,$http,$timeout) {
//	var local = "%2Fhtml%2Fpicture%2Findex.html%23%2F";from=singlemessage&isappinstalled=0#/?a=1
	var local = location.href;
	local = encodeURIComponent(local);
	var url = baseUrl+"/index.php?g=mobile&m=picture&a=get_wx_info&url="+local;
    $http.get(url).success(function (res) {
    	wxConfig(res);
    	hideSpin();
    	
    });
    $timeout.cancel(autoJump);
}

function UploadController($scope,$http,$timeout,$rootScope){
//	var local = "%2Fhtml%2Fpicture%2Findex.html%23%2Fupload";
	var local = location.href;
	local = encodeURIComponent(local);
	var url = baseUrl+"/index.php?g=mobile&m=picture&a=get_wx_info&url="+local;
    $http.get(url).success(function (res) {
    	wxConfig(res);
    	hideSpin();
    });
	$scope.dataType ="yes";
	$scope.dataText="测测夫妻相指数";
	$scope.yfpost = function(){
		var type = $scope.dataType;
		if(type == 'done')
		{
			showMsg("你已经上传过你和ta的照片了。");
		}
		else if(type == 'yes')
		{
			var data = get_form_data();
			
			$scope.dataType = 'no';
			$scope.dataText='提交中';
			var url = baseUrl+"/index.php?g=mobile&m=picture&a=send_set";
			var  transFn = function(data) {
                return $.param(data);
            };
           var postCfg = {
                headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
                transformRequest: transFn
            };
			$http.post(url,data,postCfg).success(function(result){
				if(result.error == 0)
				{
					$scope.dataType='done';
					$timeout(function(){
//						window.location.href="http://bgy2.lly360.com/html/picture2/index.html#/match";
						$rootScope.go("/match");
					},1000);
				}
				else
				{
					
					showMsg(result.content);
					$scope.dataType='yes';
					$scope.dataText='测测夫妻相指数';
				}
			});
		}
		else
		{
			showMsg("数据提交中");
		}
	}
}

function MatchController($scope,$http,$timeout,$rootScope){
//	var local = "%2Fhtml%2Fpicture%2Findex.html%23%2Fmatch";
	var local = location.href;
	local = encodeURIComponent(local);
	var url = baseUrl+"/index.php?g=mobile&m=picture&a=get_match&url="+local;
    $http.get(url).success(function (res) {
    	hideSpin();
    	if(res.match){
    		$scope.pic = res.match.pic;
    		$scope.pic1 = res.match.pic1;
    		$scope.ranks = res.match.ranks;
    		wxShareConfig(res.jssdk,res.match.id);
    	}else{
    		$rootScope.go("/");
    	}
    });
}

function ShareController($scope,$http,$routeParams,$rootScope){
//	var local = "%2Fhtml%2Fpicture%2Findex.html%23%2Fshare";
	var local = location.href;
	local = encodeURIComponent(local);
	if(!$routeParams.shareId||$routeParams.shareId==1){
		$rootScope.go("/");
	}else{
		var url = baseUrl+"/index.php?g=mobile&m=picture&a=get_share&wxid="+$routeParams.shareId+"&url="+local;
	    $http.get(url).success(function (res) {
	    	hideSpin();
	    	if(res.match){
	    		var img=new Image();
                img.src=baseUrl+"/"+res.match.pic;
                console.info(img.src);
                console.info(img.readyState);
                img.onload=function(){
                	console.info('图片的宽度为：'+img.width+'；图片的高度为：'+img.height);
                	if (img.fileSize > 0 || (img.width > 0 && img.height > 0)) {
                		console.info("2图片");
    	              } else {  
    	            		$rootScope.go("/");
    	              }  
            	}
	    		var img1=new Image();
                img1.src=baseUrl+"/"+res.match.pic1;
                img1.onload=function(){
                	console.info('图片的宽度为：'+img.width+'；图片的高度为：'+img.height);
                	if (img1.fileSize > 0 || (img1.width > 0 && img1.height > 0)) {
                    	console.info("2图片");
    	              } else {  
    	            		$rootScope.go("/");
    	              }  
            	}
                
                $scope.pic = res.match.pic;
	    		$scope.pic1 = res.match.pic1;
	    		$scope.ranks = res.match.ranks;
	    		wxShareConfig(res.jssdk,res.match.id);
	    	}else{
	    		$rootScope.go("/");
	    	}
	    });
	}
}


